// 此為 api.js 檔案 (統一管理 API)
  import axios from 'axios'
  const userRequest = axios.create({
    baseURL: 'http://localhost:3000',
    headers: { 'Content-Type': 'application/json' },
  })
  // 可以統一管理 API Call
  const postFunc = data => userRequest.post('/users', data)
  const getFunc = url => userRequest.get(url)
  // 要使用該 api 的檔案,使用 import將方法載入
  import { postFunc, getFunc } from 'api.js'; 
  run()
  async function run() {
    try {
      // 可以連 URL 都進行封裝
      const post1 = await postFunc({ name: '123' })
      const post2 = await postFunc({ name: '456' })
      
      // 上面的方法更佳 API 應存在同一個檔案作管理
      const post3 = await userRequest.post('/users', { name: '789' })
      
      const get = await getFunc('/users')
      console.table(get.data)
    } catch (error) {
      throw new Error(error)
    }
  }
  // 不設定即採用默認值 timeout 為 0
  let instance = axios.create()
  // 覆蓋原始 instance
  instance.defaults.timeout = 2500
  // 發送請求的 config
  instance.get('/longRequest', {
    timeout: 5000 // 採用此 config 設定
  })
  console.log(axios.default)
  axios.defaults.baseURL = 'url'
  axios.defaults.headers.common['Content-Type'] = '~'
  axios.defaults.headers.post['Content-Type'] = '~'
  // 自定 reject 狀態碼範圍
  const config = {
    validateStatus(status) { return status >= 200 && status < 300 },
  }
  axios.get('/user/12345')
    .catch((error) => {
      if (error.response) {
        // 當狀態碼不在 validateStatus 設定的範圍時進入
        // 有 data / status / headers 參數可用
        console.log(error.response.status)
      } else if (error.request) {
        // 發送請求,但沒有接到回應
        // error.request is an instance of XMLHttpRequest in the browser 
        // and an instance of http.ClientRequest in node.js
        console.log(error.request)
      } else {
        // 在設定 request 時出錯會進入此
        console.log('Error', error.message)
      }
      console.log(error.config)
    })
  axios({
    method: 'get',
    url: 'http://bit.ly/2mTM3nY',
    responseType: 'stream',
  })
    .then((response) => { 
      response.data.pipe(fs.createWriteStream('Img.jpg')) 
    })
axios Github
axios 中文文檔(有些省略的)
axios小筆記
使用Axios你的API都怎麼管理?